module.exports = function (app, api_prefix = '/api/logger') {
  const path = require("path");
  const fs = require("fs");
  const multer = require('multer'); // 支持 multipart/form-data 类型
  const upload = multer();

  const log4js = require('log4js');


  const logger = log4js.getLogger('logger');
  log4js.getLogger().debug(`Startup logger api_prefix=${api_prefix}`);

  const image_generator_dir = path.join(__dirname, "public/image_generator");
  // cgi, common gateway interface 通用网关接口
  // api, application programming  interface 应用程序接口
  app.use(`${api_prefix}/:level`, function (req, res, next) {
      var params = req.method == "GET" ? req.query : req.body;
      var headers = req.headers;
      res.set({
          'Content-Type': 'text/plain',
      });
      let caller = logger[req.params.level];
      if (caller) {
        caller.call(logger, req.host + " " + JSON.stringify(req.query) + " "+ JSON.stringify(req.body));
        sendResponse(req, res, 200, "ok");
      } else {
        sendResponse(req, res, 400, `logger entry [${req.params.level}] not found`);
      }
  });

  function sendResponse(req, res, status_code, msg) {
      // 把错误统一封装
      if (typeof msg === "string" && status_code != 200) {
          msg = JSON.stringify({msg: msg, code: -1, data: {}});
      }
      res.status(status_code).send(msg);
  }
}